<?php
namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Request;
use think\Validate;
use app\admin\model\Config;
use app\admin\model\About;
use app\admin\model\Like;
use app\admin\model\User;

class System extends Controller
{
    public function like_list()
    {
        $like = new Like();
        $data = $like->get_LikeList();
        $this->assign([
            'data'  =>  $data
        ]);
        return $this->fetch();
    }

    public function like_add()
    {
        $like = new Like();
        if (Request::instance()->isPost()) {
            $file = request()->file('logo');
            if($file){
                $info = $file->move(ROOT_PATH . 'public' . DS . 'images');
                if($info){
                    $data['logo'] = '/public' . '/images/' . date("Ymd") . '/' . $info->getFilename();
                }else{
                    // 上传失败获取错误信息
                    echo $file->getError();
                    $this->getError("上传出错");die;
                }
            }
            $data = input('post.');
            $data['addtime'] = time();
            $res = $like->get_LikeAdd($data);
            if ($res) {
                $this->success('新增成功');
            }else{
                $this->error('新增失败');
            }
        }
        return $this->fetch();
    }

    public function like_edit()
    {
        $like = new Like();
        $id = input('param.id');
        $like_one = $like->get_LikeOne($id);
        if (Request::instance()->isPost()) {
            $file = request()->file('logo');
            if($file){
                $info = $file->move(ROOT_PATH . 'public' . DS . 'images');
                if($info){
                    $data['logo'] = '/public' . '/images/' . date("Ymd") . '/' . $info->getFilename();
                }else{
                    // 上传失败获取错误信息
                    echo $file->getError();
                    $this->getError("上传出错");die;
                }
            }
            $data = input('post.');
            $data['addtime'] = time();
            $res = $like->get_LikeEdit($data,$id);
            if ($res) {
                $this->success('修改成功');
            }else{
                $this->error('修改失败');
            }
        }
        $this->assign([
            'data'  =>  $like_one
        ]);
        return $this->fetch();
    }

    public function like_del()
    {
        $like = new Like();
        $id = input('param.id');
        $res = $like->get_LikeDel($id);
        if ($res) {
            return 200;
        }else{
            return 400;
        }
    }

    public function system_base()
    {
        $config = new Config();
        $data = $config->get_Config();
        if (Request::instance()->isPost()) {
            $data = input('post.');
            $res = $config->Edit_Config($data);
            if ($res) {
                $this->success('修改成功');
            }else{
                $this->error('修改失败');
            }
        }
        $this->assign([
            'data'  =>  $data
        ]);
        return $this->fetch();
    }
    
    public function credential()
    {
        $user = new User();
        $data = $user->get_Credential();
        $this->assign([
            'data'  =>  $data
        ]);
        return $this->fetch();
    }
}